/**
 * 
 */
package com.starbaba.core.utils.json;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * JSON 配置工厂类
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2012-2-13 下午10:48:29
 * @version V1.0
 */
public class JsonConfigFactory {
	
	private static Map<String, JsonConfig> CONFIG_CACHE = new HashMap<String, JsonConfig>();
	
	/**
	 * 使用默认日期格式（yyyy-MM-dd HH:mm:ss）创建JsonConfig
	 * @return JsonConfig
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2012-2-14 上午08:43:59
	 * @version V1.0
	 */
	public static JsonConfig createJsonConfig() {
		return createJsonConfig(null);
	}
	
	/**
	 * 根据指定日期格式创建JsonConfig
	 * @param datePattern
	 * @return JsonConfig
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2012-2-14 上午08:44:27
	 * @version V1.0
	 */
	public static JsonConfig createJsonConfig(String datePattern) {
		if(CONFIG_CACHE.containsKey(datePattern)) {
			return CONFIG_CACHE.get(datePattern);
		}
		
		JsonValueProcessor processor = new JsonDateValueProcessor(datePattern);
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(Date.class, processor);
		config.registerJsonValueProcessor(java.sql.Date.class, processor);
		CONFIG_CACHE.put(datePattern, config);
		
		return config;
	}
}
